package org.fnzn.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.fnzn.tree.DicTree;
import org.fnzn.util.treestr.TreeUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.fnzn.aop.SystemControllerLog;
import org.fnzn.bean.SysDic;
import org.fnzn.core.Result;
import org.fnzn.service.SysDicService;
import org.fnzn.util.ResultGenerator;

@RestController
@RequestMapping("/dic")  
@EnableAutoConfiguration 
public class SysDicController {
	@Autowired
	private SysDicService sysDicService;
	@RequestMapping(value = "/find/{parentId}",produces = "application/json;charset=UTF-8",method = RequestMethod.GET)
    public Result selectList(@PathVariable String parentId){ 		
        return sysDicService.finSysDicByParentId(parentId);
    }
	@RequestMapping(value = "/{dicParentid}",produces = "application/json;charset=UTF-8",method = RequestMethod.GET)
	@SystemControllerLog(description = "查询字典信息")
	public Result selectDic(@PathVariable String dicParentid) {
		return ResultGenerator.genSuccessResult(sysDicService.selectDic(dicParentid));
	}
	
	@RequestMapping(value = "/sysDic/tree", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET })
	@SystemControllerLog(description = "查询字典树")
	public Result querySysDicTree() {
		Result result = sysDicService.querySysDicTree();
		List<SysDic>  dics = (List<SysDic>)result.getData();
		List<DicTree> trees = new ArrayList<>();
		for (SysDic d : dics) {
			DicTree node = new DicTree();
			BeanUtils.copyProperties(d, node);
			node.setParentId(d.getDicParentid());
			node.setId(d.getDicId());
			trees.add(node);
		}
		trees = TreeUtil.buildByRecursive(trees, "xxx");
		return result;
	}
	
	@RequestMapping(value = "/sysDic/page", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET })
	@SystemControllerLog(description = "查询")
	public Result querySysDicById(@RequestParam(defaultValue = "0") String parentId) {	
		if(parentId.equals("0")) {
			return ResultGenerator.genSuccessResult(new ArrayList<SysDic>());	
		}else {
			return ResultGenerator.genSuccessResult(sysDicService.querySysDicById(parentId));
		}
	}
	
	@RequestMapping(value = "/sysDic", produces = "application/json;charset=UTF-8", method = { RequestMethod.PUT })
	@SystemControllerLog(description = "修改字典")
	public Result updateSysDic(SysDic sysDic) {		
		return sysDicService.updateSysDic(sysDic);
	}
	@RequestMapping(value = "/sysDic", produces = "application/json;charset=UTF-8", method = { RequestMethod.DELETE })
	@SystemControllerLog(description = "删除字典")
	public Result deleteSysDic(SysDic sysdic) {
		return sysDicService.deleteSysDic(sysdic);
	}

	@RequestMapping(value = "/sysDic", produces = "application/json;charset=UTF-8", method = { RequestMethod.POST })
	@SystemControllerLog(description = "添加字典")
	public Result insertSysDic(SysDic sysDic){
		int save = 0;
		SysDic dic = sysDicService.selectDic(sysDic);
		if(dic==null){
			save = sysDicService.save(sysDic);
		}else{
			return ResultGenerator.genFailResult("已存在");
		}
		if(save>0){
			return ResultGenerator.genSuccessResult();
		}else{
			return ResultGenerator.genFailResult("新增失败");
		}
	}

	// 查询到指定字典值
    @RequestMapping(value = "/sysDic/getDicByDicType",produces = "application/json;charset=UTF-8",method = {RequestMethod.GET})
    @SystemControllerLog(description = "查询到指定字典值")
    public Result getDicByDicType(String dicType){
        return ResultGenerator.genSuccessResult(sysDicService.getDicByDicType(dicType));
    }
}

